//
//  KKPhotoPreViewController.m
//  KKImagePickerController
//
//  Created by Leo on 15/6/24.
//  Copyright (c) 2015年 Juvid. All rights reserved.
//

#import "KKPhotoPreViewController.h"

#import "KKImagePickerModel.h"

static NSString* KKPhotoPreItemCollectionCellIdentifier = @"KKPhotoPreItemCollectionCellIdentifier";

@interface KKPhotoPreViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout> {
    
    UICollectionView* _collectionView;
    UIButton* _sendButton;
    
    CGSize _itemCellSize;
}

@end

@implementation KKPhotoPreViewController

- (void)loadView {
    [super loadView];
    UIView* rootView = [self view];
    rootView.backgroundColor = [UIColor whiteColor];
    
    KKPhotoPreItemCellFlowLayout* flowLayout = [KKPhotoPreItemCellFlowLayout new];
    
    _collectionView = [[UICollectionView alloc] initWithFrame: [rootView bounds] collectionViewLayout: flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [rootView addSubview: _collectionView];
    
    [_collectionView registerClass: [KKPhotoPreItemCell class] forCellWithReuseIdentifier: KKPhotoPreItemCollectionCellIdentifier];
    
    {
        _sendButton = [UIButton buttonWithType: UIButtonTypeCustom];
        _sendButton.frame = CGRectMake(0, 0, 60, 40);
        _sendButton.titleLabel.font = [UIFont systemFontOfSize: 16.0];
        _sendButton.titleLabel.textAlignment = NSTextAlignmentRight;
        [_sendButton setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
        [_sendButton setTitleColor: [UIColor lightGrayColor] forState: UIControlStateHighlighted];
        [_sendButton addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(onTap:)]];
        UIBarButtonItem* sendItem = [[UIBarButtonItem alloc] initWithCustomView: _sendButton];
        
        UIBarButtonItem* spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: nil action: nil];
        
        self.toolbarItems = @[spaceItem, sendItem];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _itemCellSize = CGSizeMake(CGRectGetWidth(_collectionView.bounds), CGRectGetHeight(_collectionView.bounds) - 30);
    
    NSInteger count = [[KKImagePickerModel sharedManager] count];
    [_sendButton setTitle:[NSString stringWithFormat: @"保存(%@)", @(count)] forState: UIControlStateNormal];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear: animated];
    
    [self.navigationController setToolbarHidden: NO animated: YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear: animated];
    
    [self.navigationController setToolbarHidden: YES animated: YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -

- (void)onTap:(UITapGestureRecognizer*)tapper {
    UIView* target = tapper.view;
    if (target == _sendButton) {
        
        NSDictionary* info = @{@"type":@"save"};
        [[NSNotificationCenter defaultCenter] postNotificationName: KKImagePickerControllerNotificationActionOfSend object: nil userInfo: info];
        return;
    }
}

#pragma mark -
#pragma mark - UICollectionView methods

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [[KKImagePickerModel sharedManager] count];
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    KKPhotoPreItemCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier: KKPhotoPreItemCollectionCellIdentifier forIndexPath: indexPath];
    
    ALAsset* assetItem = [[KKImagePickerModel sharedManager] itemOfIndex: indexPath.row];
    
    UIImage* image = [UIImage imageWithCGImage: assetItem.aspectRatioThumbnail];
    
    cell.itemImageView.image = image;
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return _itemCellSize;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    return UIEdgeInsetsMake(25, 0, 25, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0.0;
}

@end


#pragma mark -
#pragma mark -

@interface KKPhotoPreItemCell() <UIScrollViewDelegate> {
    
    UIScrollView *_pannelScrollView;
}

@end

@implementation KKPhotoPreItemCell

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame: frame];
    if (self) {
        
        UIView* contentView = [self contentView];
        
        _pannelScrollView = [[UIScrollView alloc] initWithFrame: [contentView bounds]];
        _pannelScrollView.delegate = self;
        _pannelScrollView.maximumZoomScale = 3.0;
        _pannelScrollView.minimumZoomScale = 0.6;
        _pannelScrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        [contentView addSubview: _pannelScrollView];
        
        _itemImageView = [[UIImageView alloc] initWithFrame: [_pannelScrollView bounds]];
        _itemImageView.contentMode = UIViewContentModeCenter;
        _itemImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [_pannelScrollView addSubview: _itemImageView];
    }
    return self;
}

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    
    return _itemImageView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    
}

@end

#pragma mark -
#pragma mark -

@implementation KKPhotoPreItemCellFlowLayout

- (id)init {
    self = [super init];
    if (self) {
        self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.minimumInteritemSpacing = 0.0f;
        self.minimumLineSpacing = 0.0f;
    }
    return self;
}

@end
